package com.egreen.terminal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egreen.common.Result;
import com.egreen.common.States;
import com.egreen.common.pojo.entity.terminal.Terminalcount;
import com.egreen.terminal.dao.TerminalcountDao;
import com.egreen.terminal.service.ITerminalcountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cloudJiang
 * @since 2022-07-16
 */
@Slf4j
@Service
public class TerminalcountServiceImpl extends ServiceImpl<TerminalcountDao, Terminalcount> implements ITerminalcountService {
    @Autowired
    TerminalcountDao terminalcountDao;

    /**
     * 终端注册接口，从请求中获取终端序列号serialNumber以及状态码state
     * 创建终端terminal对象存入属性，查询数据库是否存在相同序列号终端已经注册，是则返回对应Result,否则插入数据库，插入成功则返回insert=1，否则插入不成功
     * @param serialNumber
     * @param state
     * @param city 终端所在城市（例如昆明市）
     * @param district 终端所在区域（例如呈贡区）
     * @param community 终端所在社区（例如呈贡区雨花毓秀小区）
     * @param locationDetails 终端详细地址（例如呈贡区雨花毓秀小区5栋人行道旁）
     * @return
     */
    @Override
    public Result registerTerminal(String serialNumber,String state,String city,String district, String community, String locationDetails) {
        Terminalcount terminalcount = new Terminalcount();
        terminalcount.setSerialNumber(serialNumber);
        terminalcount.setState(state);
        terminalcount.setCity(city);
        terminalcount.setDistrict(district);
        terminalcount.setCommunity(community);
        terminalcount.setLocationDetails(locationDetails);

        QueryWrapper<Terminalcount> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("serial_number",serialNumber);
        Integer existedTerminalNumber = terminalcountDao.selectCount(queryWrapper);
        if (existedTerminalNumber == 0) {
            int insert = terminalcountDao.insert(terminalcount);
            if (insert != 1) {
                return Result.Error(States.ERR_TERMINALCOUNT_TERMINAL_INSERT);
            }else return Result.OK();
        }else return Result.Error(States.ERR_TERMINALCOUNT_TERMINAL_REGISTER_EXISTED);

    }


    /**
     * 终端登录
     * 根据终端序列号查询状态是否可用且终端地址不缺失，缺失则不可用
     * @param serialNumber
     * @return
     */
    @Override
    public Result loginTerminal(String serialNumber) {
        QueryWrapper<Terminalcount> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("serial_number",serialNumber);
        Terminalcount terminalcount = terminalcountDao.selectOne(queryWrapper);
        if (terminalcount != null){
            if (terminalcount.getState().equals("可用")){
                if(terminalcount.getCity().isBlank()&&terminalcount.getDistrict().isBlank()&&terminalcount.getCommunity().isBlank()&&terminalcount.getLocationDetails().isBlank()){
                    return Result.Error(States.ERR_TERMINALCOUNT_TERMINAL_ADDRESS_MISSING);
                }else return Result.OK();
            }else return Result.Error(States.ERR_TERMINALCOUNT_TERMINAL_STATE_ABNORMAL);
        }else return Result.Error(States.ERR_TERMINALCOUNT_TERMINAL_LOGIN_NOTEXISTED);
    }

    /**
     * 获取到终端序列号对数据库进行删除实现终端下线
     * @param serialNumber
     * @return
     */
    @Override
    public Result terminalLogout(String serialNumber) {
        QueryWrapper<Terminalcount> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("serial_number",serialNumber);
        int deleteNumber = terminalcountDao.delete(queryWrapper);
        if (deleteNumber != 1){
            return Result.Error(States.ERR_TERMINALCOUNT_TERMINAL_DELETE);
        }else return Result.OK();
    }

    /**
     *终端状态更改
     * @param serialNumber
     * @param state
     * @return
     */
    @Override
    public Result terminalStateModification(String serialNumber, String state) {
        Terminalcount modifingTerminal = new Terminalcount();
        modifingTerminal.setSerialNumber(serialNumber).setState(state);
        QueryWrapper<Terminalcount> queryWrapper = new QueryWrapper();
        queryWrapper.likeRight("serial_number",serialNumber);
        int updateNumber = terminalcountDao.update(modifingTerminal, queryWrapper);
        if (updateNumber != 1){
            return Result.Error(States.ERR_TERMINALCOUNT_TERMINAL_UPDATA);
        }else
            return Result.OK();
    }

    /**
     * 获取全部终端列表
     * @return
     */
    @Override
    public List<Map<String, Object>> TerminalListQuery() {
        List<Map<String, Object>> terminalList = terminalcountDao.selectMaps(null);
        return terminalList;
    }
}
